<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆vin" prop="vehicleVin">
        <el-input
          v-model="queryParams.vehicleVin"
          placeholder="请输入车辆vin"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零售单号" prop="retailOrderNo">
        <el-input
          v-model="queryParams.retailOrderNo"
          placeholder="请输入零售单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商名称" prop="dealerName">
        <el-input
          v-model="queryParams.dealerName"
          placeholder="请输入经销商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="customerType">
        <el-select v-model="queryParams.customerType" placeholder="请选择客户类型" clearable>
          <el-option
            v-for="dict in dict.type.whole_sale_customer_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="PDI 日期" prop="pdiDate">
        <el-date-picker clearable
          v-model="queryParams.pdiDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择PDI 日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="销售日期" prop="saleDate">
        <el-date-picker clearable
          v-model="queryParams.saleDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择销售日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上报日期" prop="reportDate">
        <el-date-picker clearable
          v-model="queryParams.reportDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择上报日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上报人姓名" prop="reportPersonName">
        <el-input
          v-model="queryParams.reportPersonName"
          placeholder="请输入实销上报人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['whole:report:add']"-->
<!--        >上报</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['whole:report:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['whole:report:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['whole:report:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="实销上报id" align="center" prop="id" />
      <el-table-column label="车辆vin" align="center" prop="vehicleVin" />
      <el-table-column label="零售单号" align="center" prop="retailOrderNo" />
      <el-table-column label="经销商名称" align="center" prop="dealerName" />
      <el-table-column label="客户类型" align="center" prop="customerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_sale_customer_type" :value="scope.row.customerType"/>
        </template>
      </el-table-column>
      <el-table-column label="PDI 日期" align="center" prop="pdiDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pdiDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售日期" align="center" prop="saleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实销上报日期" align="center" prop="reportDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上报人姓名" align="center" prop="reportPersonName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查阅详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['whole:report:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实销上报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车辆vin" prop="vehicleVin">
          <el-input v-model="form.vehicleVin" placeholder="请输入车辆vin" :disabled="title!='添加实销上报'"/>
        </el-form-item>
        <el-form-item label="零售单号" prop="retailOrderNo">
          <el-input v-model="form.retailOrderNo" placeholder="请输入零售单号" :disabled="title!='添加实销上报'"/>
        </el-form-item>
        <el-form-item label="客户类型" prop="customerType">
          <el-select v-model="form.customerType" placeholder="请选择客户类型">
            <el-option
              v-for="dict in dict.type.whole_sale_customer_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
              :disabled="title!='添加实销上报'"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="PDI日期" prop="pdiDate">
          <el-date-picker clearable
            v-model="form.pdiDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择PDI 日期"
            :disabled="title!='添加实销上报'"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="销售日期" prop="saleDate">
          <el-date-picker clearable
            v-model="form.saleDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择销售日期"
            :disabled="title!='添加实销上报'"
          ></el-date-picker>
        </el-form-item>
        <el-divider content-position="center">实销上报客户信息</el-divider>
        <!-- 新增时有 添加/删除按钮、自定义table -->
        <el-row v-if="title=='添加实销上报'" :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDmsSaleReportCustomerInfo">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDmsSaleReportCustomerInfo">删除</el-button>
          </el-col>
        </el-row>
        <el-table v-if="title=='添加实销上报'" :data="dmsSaleReportCustomerInfoList" :row-class-name="rowDmsSaleReportCustomerInfoIndex" @selection-change="handleDmsSaleReportCustomerInfoSelectionChange" ref="dmsSaleReportCustomerInfo">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="客户姓名" prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入客户姓名" />
            </template>
          </el-table-column>
          <el-table-column label="客户电话" prop="phone" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.phone" placeholder="请输入客户电话" />
            </template>
          </el-table-column>
          <el-table-column label="客户生日" prop="birthday" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.birthday" type="date" value-format="yyyy-MM-dd" placeholder="请选择客户生日" />
            </template>
          </el-table-column>
          <el-table-column label="客户年龄" prop="age" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.age" placeholder="请输入客户年龄" />
            </template>
          </el-table-column>
          <el-table-column label="客户性别" prop="gender" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.gender" placeholder="请选择客户性别">
                <el-option
                  v-for="dict in dict.type.gender"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="客户货币" prop="currency" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.currency" placeholder="请选择客户货币">
                <el-option
                  v-for="dict in dict.type.currency"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="客户国籍" prop="nationality" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.nationality" placeholder="请选择客户国籍">
                <el-option
                  v-for="dict in dict.type.nationality"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="邮件地址" prop="email" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.email" placeholder="请输入邮件地址" />
            </template>
          </el-table-column>
          <el-table-column label="客户地址" prop="address" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.address" placeholder="请输入客户地址" />
            </template>
          </el-table-column>
          <el-table-column label="购车价格" prop="buyPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.buyPrice" placeholder="请输入购车价格" />
            </template>
          </el-table-column>
          <el-table-column label="支付方式" prop="paymentMethod" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.paymentMethod" placeholder="请选择支付方式">
                <el-option
                  v-for="dict in dict.type.pay_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="车牌号" prop="plateNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.plateNumber" placeholder="请输入车牌号" />
            </template>
          </el-table-column>
          <el-table-column label="上牌日期" prop="plateDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.plateDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择上牌日期" />
            </template>
          </el-table-column>
          <el-table-column label="婚姻状况" prop="maritalStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.maritalStatus" placeholder="请选择婚姻状况">
                <el-option
                  v-for="dict in dict.type.marital_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="月薪" prop="salary" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.salary" placeholder="请输入月薪" />
            </template>
          </el-table-column>
          <el-table-column label="是否首次购车" prop="isFirstTime" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.isFirstTime" placeholder="请选择是否首次购车">
                <el-option
                  v-for="dict in dict.type.is_first_buy_car"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
        <!-- 修改时只显示客户信息 -->
        <el-table v-if="title!='添加实销上报'" v-loading="loading" :data="dmsSaleReportCustomerInfoList" :row-class-name="rowDmsSaleReportCustomerInfoIndex" @selection-change="handleDmsSaleReportCustomerInfoSelectionChange"  ref="dmsSaleReportCustomerInfo">
<!--          <el-table-column v-if="this.title =='添加车辆采购' " type="selection" width="55" align="center" />-->
          <el-table-column label="序号" align="center" prop="index" width="50"/>-->
          <el-table-column label="id" align="center" prop="id" />
          <el-table-column label="客户姓名" align="center" prop="name" />
          <el-table-column label="客户电话" align="center" prop="phone" />
          <el-table-column label="客户生日" align="center" prop="birthday" />
          <el-table-column label="客户年龄" align="center" prop="age" />
          <el-table-column label="客户性别" align="center" prop="gender" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.gender" :value="scope.row.gender"/>
            </template>
          </el-table-column>
          <el-table-column label="客户货币" align="center" prop="currency" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.currency" :value="scope.row.currency"/>
            </template>
          </el-table-column>
          <el-table-column label="客户国籍" align="center" prop="nationality" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.nationality" :value="scope.row.nationality"/>
            </template>
          </el-table-column>
          <el-table-column label="邮件地址" align="center" prop="email" />
          <el-table-column label="客户地址" align="center" prop="address" />
          <el-table-column label="购车价格" align="center" prop="buyPrice" />
          <el-table-column label="支付方式" align="center" prop="paymentMethod" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pay_type" :value="scope.row.paymentMethod"/>
            </template>
          </el-table-column>
          <el-table-column label="车牌号" align="center" prop="plateNumber" />
          <el-table-column label="上牌日期" align="center" prop="plateDate" />
          <el-table-column label="婚姻状况" align="center" prop="maritalStatus" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.marital_status" :value="scope.row.maritalStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="月薪" align="center" prop="salary" />
          <el-table-column label="是否首购" align="center" prop="isFirstTime" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.is_first_buy_car" :value="scope.row.isFirstTime"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReport, getReport, delReport, addReport, updateReport } from "@/api/whole/reportQuery";

export default {
  name: "Report",
  dicts: ['marital_status', 'is_first_buy_car', 'pay_type', 'nationality', 'whole_sale_customer_type', 'gender', 'currency'],
  data() {
    return {
      // 用于标记客户信息表单必填是否填写
      isFormValid: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDmsSaleReportCustomerInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实销上报表格数据
      reportList: [],
      // 实销上报客户信息表格数据
      dmsSaleReportCustomerInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleVin: null,
        retailOrderNo: null,
        dealerName: null,
        customerType: null,
        pdiDate: null,
        saleDate: null,
        reportDate: null,
        reportPersonName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vehicleVin: [
          { required: true, message: "车辆vin不能为空", trigger: "blur" }
        ],
        retailOrderNo: [
          { required: true, message: "零售单号不能为空", trigger: "blur" }
        ],
        customerType: [
          { required: true, message: "客户类型不能为空", trigger: "blur" }
        ],
        pdiDate: [
          { required: true, message: "PDI质检日期不能为空", trigger: "blur" }
        ],
        saleDate: [
          { required: true, message: "销售日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询实销上报列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vehicleVin: null,
        retailOrderNo: null,
        dealerId: null,
        dealerName: null,
        customerType: null,
        pdiDate: null,
        saleDate: null,
        reportDate: null,
        reportPersonId: null,
        reportPersonName: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      this.dmsSaleReportCustomerInfoList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实销上报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data;
        this.dmsSaleReportCustomerInfoList = response.data.dmsSaleReportCustomerInfoList;
        this.open = true;
        this.title = "修改实销上报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.dmsSaleReportCustomerInfoList = this.dmsSaleReportCustomerInfoList;
          if (this.form.id != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //判断客户字段必填字段是否填写
            this.dmsSaleReportCustomerInfoList.forEach(customerInfo => {
              if (!customerInfo.name || customerInfo.name.trim() === '') {
                this.$message.error('客户姓名不能为空！');
                this.isFormValid = false;
                return;
              }
              if (!customerInfo.phone || isNaN(customerInfo.phone) || customerInfo.phone <= 0) {
                this.$message.error('客户电话不能为空且必须为11位！');
                this.isFormValid = false;
                return;
              }
              if (!customerInfo.currency || isNaN(customerInfo.currency) || customerInfo.currency < 0) {
                this.$message.error('货币类型不能为空！');
                this.isFormValid = false;
                return;
              }
              if (!customerInfo.nationality || isNaN(customerInfo.nationality) || customerInfo.nationality < 0) {
                this.$message.error('国籍不能为空！');
                this.isFormValid = false;
                return;
              }
              if (!customerInfo.buyPrice || isNaN(customerInfo.buyPrice) || customerInfo.buyPrice <= 0) {
                this.$message.error('购车价格不能为空！');
                this.isFormValid = false;
                return;
              }
              if (!customerInfo.paymentMethod || isNaN(customerInfo.paymentMethod) || customerInfo.paymentMethod < 0) {
                this.$message.error('支付方式不能为空！');
                this.isFormValid = false;
                return;
              }
              if (!customerInfo.plateNumber || customerInfo.plateNumber.trim() === '') {
                this.$message.error('车牌号不能为空！');
                this.isFormValid = false;
                return;
              }
              if (!customerInfo.isFirstTime || isNaN(customerInfo.isFirstTime) || customerInfo.isFirstTime < 0) {
                this.$message.error('是否首次购车不能为空！');
                this.isFormValid = false;
                return;
              }
            });
            //必填字段都填写，执行新增表单
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实销上报编号为"' + ids + '"的数据项？').then(function() {
        return delReport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 实销上报客户信息序号 */
    rowDmsSaleReportCustomerInfoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 实销上报客户信息添加按钮操作 */
    handleAddDmsSaleReportCustomerInfo() {
      let obj = {};
      obj.name = "";
      obj.phone = "";
      obj.birthday = "";
      obj.age = "";
      obj.gender = "";
      obj.currency = "";
      obj.nationality = "";
      obj.email = "";
      obj.address = "";
      obj.buyPrice = "";
      obj.paymentMethod = "";
      obj.plateNumber = "";
      obj.plateDate = "";
      obj.remark = "";
      obj.maritalStatus = "";
      obj.salary = "";
      obj.isFirstTime = "";
      this.dmsSaleReportCustomerInfoList.push(obj);
    },
    /** 实销上报客户信息删除按钮操作 */
    handleDeleteDmsSaleReportCustomerInfo() {
      if (this.checkedDmsSaleReportCustomerInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的实销上报客户信息数据");
      } else {
        const dmsSaleReportCustomerInfoList = this.dmsSaleReportCustomerInfoList;
        const checkedDmsSaleReportCustomerInfo = this.checkedDmsSaleReportCustomerInfo;
        this.dmsSaleReportCustomerInfoList = dmsSaleReportCustomerInfoList.filter(function(item) {
          return checkedDmsSaleReportCustomerInfo.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDmsSaleReportCustomerInfoSelectionChange(selection) {
      this.checkedDmsSaleReportCustomerInfo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('whole/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
